package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (Gruppe B) 
 * @version (1.0)
 */
public class AddressBookUI
{   
    /**
     * Instace variables.
     */
    private AddressCtr addrCtr;
    private int trueId;
    
    /**
     * Constructor which does nothing
     */
    public AddressBookUI()
    {
       
    }
    
    /**
     * Creates an object of AddressCtr.
     * Calls the addressMenu method.
     */
    public void start()
    {
        addrCtr = new AddressCtr();
        addressMenu();
    }
    
    /**
     * Starts the address menu which gives you some options about what you can do.
     */
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   
              //show person              
              trueId = inputPersonsID();
              addrCtr.getPerson(trueId);
              System.out.println("Is this the person you were looking for?");
              System.out.println("If yes type ok else type no");
              Scanner keyboard = new Scanner(System.in);
              String input = keyboard.nextLine();              
          }
          else
          {
              if(choice == 2)
              {
                    //create person                   
                    String name = inputName();
                    String address = inputAddress();
                    String postalCode = inputPostalCode();
                    String city = inputCity();
                    String phone = inputPhone();
                    addrCtr.createPerson(name, address, postalCode, city, phone);                    
              }  
              else
              {
                  if(choice == 3)
                  {
                      //delete person
                      trueId = inputPersonsID();
                      addrCtr.deletePerson(trueId);
                      Scanner keyboard = new Scanner(System.in);
                      String input = keyboard.nextLine(); 
                  }  
                  else
                  {
                      if(choice == 4)
                      {
                          // update person
                          trueId = inputPersonsID();
                          String name = inputName();
                          String address = inputAddress();
                          String postalCode = inputPostalCode();
                          String city = inputCity();
                          String phone = inputPhone();
                          addrCtr.updatePerson(trueId, name, address, postalCode, city, phone);
                      }
                      else
                      {
                          if(choice == 5)
                          {
                              // listAllPersons();
                              addrCtr.listAllPersons();
                              Scanner keyboard = new Scanner(System.in);
                              String input = keyboard.nextLine(); 
                          }
                          else
                          {
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    /**
     * Prints out the options in the menu.
     */
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
        
    /**
     * Returns the id of a person if the id exist
     */
    public int inputPersonsID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
      
    /**
     * Sets the input as name.
     */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    /**
     * Sets the input as postal code.
     */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * Sets the input as city.
     */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    
    /**
     * Sets the input as phone number
     */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    /**
     * Sets the input as address
     */
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }  
}
            
        
